/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.test;

import java.util.Properties;
import org.balisunrise.common.DAO;
import org.balisunrise.dao.HDAO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Glauco
 */
public class HibernateTestUtil {

    private static final SessionFactory sessionFactory;
    private static final HDAO DAO;

    static {
        try {

            // propriedades
            Properties p = new Properties();

            // Load settings from hibernate.properties
            AnnotationConfiguration cfg = new AnnotationConfiguration();

            p.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
            p.setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/balisunrise");
            p.setProperty("hibernate.connection.username", "postgres");
            p.setProperty("hibernate.connection.password", "master");
            p.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            p.setProperty("show_sql", "true");
            p.setProperty("format_sql", "true");
            p.setProperty("hibernate.c3p0.max_statements", "100");
            p.setProperty("hibernate.hbm2ddl.auto", "update");
            //p.setProperty("", "");
            cfg.addProperties(p);

            cfg.addAnnotatedClass(SimpleEntity.class);
            /*
            cfg.addAnnotatedClass();
            */
            sessionFactory = cfg.buildSessionFactory();

            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            // new Configuration().configure().buildSessionFactory();
            // new AnnotationConfiguration().
            // configure().
            // buildSessionFactory();

        } catch (Throwable ex) {
            // Log the exception.
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

        DAO = new HDAOTest();
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

    public static DAO<Session> getDAO(){
        return DAO;
    }
}
